<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>学生成绩分析-云校通</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<script src="<%=basePath %>/js/echarts-3.8.4.js"></script>
<body ontouchstart>
	<div class="weui_tab tab-bottom">
		<div class="weui-header bg-blue"> 
	  		<div class="weui-header-left"> <a onclick="back()" href="javascript:" class="icon icon-109 f-white">返回</a> </div>
	    	<h1 class="weui-header-title">${student.name }同学成绩分析</h1>
	    </div>
		<div class="weui_tab_bd">
			<div class="weui_cells afterNone afterNoneBefore mt0">
          		<div class="weui_cell lblue bd-t0"> 
		            <div class="weui_cell_bd weui_cell_primary">
		                <p class=""><span id="newestExam"></span> 考试成绩分析</p>
		            </div>
	          	</div>
	          	<div class="weui_cell bd-t0">
	          		<div id="currExam" style="width: 100%;height:320px;"></div>
	          	</div>
	        </div>
	        <div class="weui_cells mt0" id="examScoreList">
      		</div>
	        <div class="weui_cells afterNone afterNoneBefore mt0">
	          	<div class="weui_cell lblue bd-t0"> 
		            <div class="weui_cell_bd weui_cell_primary">
		                <p class=""><span class="schoolYear"></span> 综合成绩分析</p>
		            </div>
	          	</div>
	          	<div class="weui_cell bd-t0">
	          		<div id="currTermExam" style="width: 100%;height:320px;"></div>
	          	</div>
      		</div>
      		
      		<div class="weui_cells bd-t0 mt0 mb40">
      			<div class="weui_cell bd-t0" id="prizeList">
	          		<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/huojiangdengji@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15"><span class="schoolYear"></span> 获奖荣誉</p>
					</div>
	          	</div>
      		</div>
      		
      		<div class="weui_cells bd-t0 mt0 mb40">
      			<div class="weui_cell bd-t0" id="schoolPrizeList">
	          		<div class="weui_cell_hd">
						<img src="<%=basePath %>/images/huojiangdengji@3x.png" style="width:18px;margin-right:5px;display:block">
					</div>
					<div class="weui_cell_hd">
						<p class="pr15"><span class="schoolYear"></span> 校内奖项</p>
					</div>
	          	</div>
      		</div>
		</div>
	</div>
</body>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script type="text/javascript">
	$(function() {
		currExam();
	})
	function currExam() {
		$.ajax({
    		url: basePath + "/student/groupup/studentScore/query",
    		type: 'POST',
    		dataType : "json",
    		data: {
    			studentId: "${student.id}",
    			examId: "${examId}"
    		},
    		success: function(resp) {
    			if(resp.length == 0) return;
    			$("#newestExam").html(resp[0].examName);
    			var array = getSubjectScoreArrray(resp[0]);
    			var scoreListHtml = '<div class="weui_cell score_item">\
	          		<div class="weui_cell_hd weui_center_50 f-14"><b>学科</b></div>\
	          		<div class="weui_cell_hd weui_center_50 f-14"><b>分数</b></div>\
	          		</div>';
    			for(var i in array) {
    				scoreListHtml += '<div class="weui_cell score_item">\
		          		<div class="weui_cell_hd weui_center_50 f-14">'+array[i].subjectName+'</div>\
		          		<div class="weui_cell_hd weui_center_50 f-14">'+array[i].score+'</div>\
		          	</div>';
    			}
    			$(".score_item").remove();
    			$("#examScoreList").html(scoreListHtml);
    			var array = getSubjectScoreArrray(resp[0]);
	          	currExamReport(array);
	          	termExam(resp[0].termName);
	          	prizeList(resp[0].termName);
    		},
    		error: function(data) {
    			$.alert("系统异常,请联系管理员", "");
    		}
    	});
	}
	function termExam(termName) {
		$(".schoolYear").html(termName);
		var termYear = termName.substr(0,4);
		$.ajax({
    		url: basePath + "/student/groupup/studentScore/query",
    		type: 'POST',
    		dataType : "json",
    		data: {
    			studentId: "${student.id}",
    			termYear: termYear
    		},
    		success: function(rspData) {
    			if(rspData.length == 0) return;
    			currTermExamReport(rspData);
    		},
    		error: function(data) {
    			$.alert("系统异常,请联系管理员", "");
    		}
    	});
	}
	function prizeList(termName) {
		var termYear = termName.substr(0,4);
		$.ajax({
    		url: basePath + "/student/groupup/prizeList",
    		type: 'POST',
    		dataType : "json",
    		data: {
    			studentId: "${student.id}",
    			termYear: termYear
    		},
    		success: function(respData) {
    			if(respData != null) {
    				var prizeList = '', schoolPrizeList = '';
    				for(var i in respData.fruits) {
    					prizeList += '<div class="weui_cell bd-t0" style="-webkit-align-items: none;align-items: left;padding-top: 0px;">\
   	    	          		<div class="weui_cell_hd f15 f-gray4e weui_75">'+respData.fruits[i].competitionName+'</div>\
   	    	          		<div class="weui_cell_hd f15 f-gray4e weui_center_25">'+respData.fruits[i].awardLevel + " " + respData.fruits[i].awardGrade+'</div>\
   	    	          	</div>';
    				}
    				$("#prizeList").after(prizeList);
    				for(var i in respData.prizes) {
    					schoolPrizeList += '<div class="weui_cell bd-t0" style="-webkit-align-items: none;align-items: left;padding-top: 0px;">\
   	    	          		<div class="weui_cell_hd f15 f-gray4e">'+respData.prizes[i].prizeName+'</div>\
   	    	          	</div>';
    				}
    				$("#schoolPrizeList").after(schoolPrizeList);
    			}
    		},
    		error: function(data) {
    			$.alert("系统异常,请联系管理员", "");
    		}
    	});
	}
	function currExamReport(data) {
		var indicatorArray = [], scoreArray = [];
		for(var i in data) {
			if((parseInt(i)+1) < data.length) {
				indicatorArray.push({name: data[i].subjectName, max: data[i].maxScore});
				scoreArray.push(data[i].score);
			}
		}
		//基于准备好的dom，初始化echarts实例
		var myChart = echarts.init(document.getElementById('currExam'));
		option = {
		    tooltip: {},
		    radar: {
		        name: {
		            textStyle: {
		                color: '#fff',
		                backgroundColor: '#999',
		                borderRadius: 3,
		                padding: [3, 5]
		           }
		        },
		        indicator: indicatorArray,
	            radius: 120
		    },
		    series: [{
		    	name: "分数",
		        type: 'radar',
		        radarIndex: 0,
		        data : [{
		        	value: (function () {
			        	return scoreArray;
			        })()
		        }]
		    }]
		};
		// 使用刚指定的配置项和数据显示图表。
	    myChart.setOption(option);
	}
	
	function currTermExamReport(list) {
		//基于准备好的dom，初始化echarts实例
		var myChart = echarts.init(document.getElementById('currTermExam'));
		// 使用刚指定的配置项和数据显示图表。
		myChart.setOption(getOptionForList(list));
	}
	function getSubjectScoreArrray(result) {
		var array = [];
		if(result.chineseScore) array.push({subjectName: "语文",score: result.chineseScore,ranking: result.classRanking, maxScore: 120});
		if(result.mathScore) array.push({subjectName: "数学",score: result.mathScore,ranking: result.mathRanking, maxScore: 120});
		if(result.englishScore) array.push({subjectName: "英语",score: result.englishScore,ranking: result.englishRanking, maxScore: 120});
		if(result.physicsScore) array.push({subjectName: "物理",score: result.physicsScore,ranking: result.physicsRanking, maxScore: 100});
		if(result.chemistryScore) array.push({subjectName: "化学",score: result.chemistryScore,ranking: result.chemistryRanking, maxScore: 100});
		if(result.biologyScore) array.push({subjectName: "生物",score: result.biologyScore,ranking: result.biologyRanking, maxScore: 100});
		if(result.politicsScore) array.push({subjectName: "政治",score: result.politicsScore,ranking: result.politicsRanking, maxScore: 50});
		if(result.historyScore) array.push({subjectName: "历史",score: result.historyScore,ranking: result.historyRanking, maxScore: 50});
		if(result.geographyScore) array.push({subjectName: "地理",score: result.geographyScore,ranking: result.geographyRanking, maxScore: 100});
		if(result.sportsScore) array.push({subjectName: "体育",score: result.sportsScore,ranking: result.sportsRanking, maxScore: 25});
		if(result.politicalHistoryScore) array.push({subjectName: "政史",score: result.politicalHistoryScore,ranking: result.politicalHistoryRanking, maxScore: 100});
		if(result.sumScore) array.push({subjectName: "总分",score: result.sumScore,ranking: result.gradeRanking,avgScore: ''});
		for(var i in array) {
			for(var j in result.scoreAvgList) {
				if(result.scoreAvgList[j].subjectName == array[i].subjectName) {
					array[i].avgScore = parseInt(result.scoreAvgList[j].avgScore);
					break;
				}
			}
		}
		return array;
	}
	function getSubject(list) {
		var subject = [], map = new Map();
		for(var i in list) {
			var array = getSubjectScoreArrray(list[i]);
			for(var j in array) {
				if(!map.get(array[j].subjectName) && ("总分" != array[j].subjectName)){
					map.set(array[j].subjectName, true);
					subject.push(array[j].subjectName);
				}
			}
		}
		subject.push("总分");
		return subject;
	}
	function getOptionForList(list) {
		var legendArray = getSubject(list);
		option = {
		    tooltip: {
		        trigger: 'axis'
		    },
		    legend: {
		        data: legendArray
		    },
		    grid: {
		    	hoverable:true,
		    	bottom: '25%',
		    	top: 100
		    },
		    xAxis: {
		    	name: "考试",
		        type: 'category',
		        boundaryGap: false,
		        data: (function() {
		        	var array = [];
		        	for(var i = list.length - 1; i >= 0; i--) {
		        		var examName = list[i].examName;
		        		array.push(examName);
		        	}
		        	return array;
		        }()),
		        axisLabel: {
		        	interval: 0, 
		        	rotate: 80
		        }
		    },
		    yAxis: {
		        type: 'value',
		        name: "分数"
		    },
		    series: (function() {
				var dataArray = [];
				for(var i in legendArray) {
					dataArray.push({
			        	name: legendArray[i],
			            type:'line',
			            symbol: 'arrow',
			            symbolSize: 15,
			            data: []
					});
				}
				for(var i = list.length - 1; i >= 0; i--) {
					array = getSubjectScoreArrray(list[i]);
					for(var j in dataArray) {
						var exists = false; 
						for(var k in array) {
							if(array[k].subjectName == dataArray[j].name) {
								dataArray[j].data.push(array[k].score);
								exists = true;
							}
						}
						if(!exists) {
							dataArray[j].data.push("-");
						}
					}
				}
				return dataArray;
		    })()
		};
		return option;
	}
</script>
</html>